<?php
namespace app\console\controller;

use think\Config;
use think\Db;
use think\Validate;
use think\Request;
use app\console\model\Manager;

class Base extends \think\Controller
{
    public function _initialize()
    {
        if (!session('?adminInfo')) {
            return $this->redirect('Login/login');
        }

        //User information
        $this->user = Manager::alias('a')
        	->where('a.id', session('adminInfo')['id'])
        	->join('jk_roles r', 'a.role_id = r.id', 'LEFT')
        	->field('a.id, a.name, a.update_time, a.role_id, r.name as role_name, r.accesses')
        	->find();

        //判断权限
        $request = \think\Request::instance();
        if ($this->user->role_id) {
            if ($this->user->role_id == 0) {
                return true;
            }
            $accesses = \think\Db::table('jk_roles')->where('id', $this->user->role_id)->find();

            $accessesArray = (explode(",", $accesses['accesses']));
        // dump(json_decode( json_encode( $accessesArray),true));

            foreach ($accessesArray as $key => $value) {
                $accessesArrayStr = (explode("/", $value));
                $accessesArrayModule[] = $accessesArrayStr[0];
                $accessesArrayController[] = $accessesArrayStr[1];
                $accessesArrayAction[] = $accessesArrayStr[2];
            }
            session('accessesArray', $accessesArray);
            session('accessesArrayModule', $accessesArrayModule);
            session('accessesArrayController', $accessesArrayController);
            session('accessesArrayAction', $accessesArrayAction);

            $access = strtolower($request->module()).'/'.strtolower($request->controller()).'/'.strtolower($request->action());
        // dump(json_decode( json_encode( $access),true));exit;
            
            if (!in_array($access, $accessesArray)) {   //无权限的情况
                $menu = input('menu');
                if($menu == 1){ //点击左侧菜单的情况
                    return $this->redirect('console/authority/no_authority');
                } else{
                    $this->authority = 'no';
                }
            } else{
                //有权限的情况
                $this->authority = 'ok';
            }
        }

        //更新最后操作的时间戳，启用登录过时重新登录
        Manager::where('id', $this->user->id)->update(['update_time' => time()]);
        $net_time = strtotime($this->user->update_time);
        $max_time = $net_time + 1800;
        if($max_time < time()){
            return $this->redirect('Login/lockscreen');
        }

        $this->time_list = array(
            array('id'=>'00', 'name'=>'0:00~1:00'),
            array('id'=>'01', 'name'=>'1:00~2:00'),
            array('id'=>'02', 'name'=>'2:00~3:00'),
            array('id'=>'03', 'name'=>'3:00~4:00'),
            array('id'=>'04', 'name'=>'4:00~5:00'),
            array('id'=>'05', 'name'=>'5:00~6:00'),
            array('id'=>'06', 'name'=>'6:00~7:00'),
            array('id'=>'07', 'name'=>'7:00~8:00'),
            array('id'=>'08', 'name'=>'8:00~9:00'),
            array('id'=>'09', 'name'=>'9:00~10:00'),
            array('id'=>'10', 'name'=>'10:00~11:00'),
            array('id'=>'11', 'name'=>'11:00~12:00'),
            array('id'=>'12', 'name'=>'12:00~13:00'),
            array('id'=>'13', 'name'=>'13:00~14:00'),
            array('id'=>'14', 'name'=>'14:00~15:00'),
            array('id'=>'15', 'name'=>'15:00~16:00'),
            array('id'=>'16', 'name'=>'16:00~17:00'),
            array('id'=>'17', 'name'=>'17:00~18:00'),
            array('id'=>'18', 'name'=>'18:00~19:00'),
            array('id'=>'19', 'name'=>'19:00~20:00'),
            array('id'=>'20', 'name'=>'20:00~21:00'),
            array('id'=>'21', 'name'=>'21:00~22:00'),
            array('id'=>'22', 'name'=>'22:00~23:00'),
            array('id'=>'23', 'name'=>'23:00~24:00'),
        );

    }

}
